package kit.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import kit.core.Core;
import kit.core.ShellException;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class RoundAnswerPanel extends KitPanel {
	private JTextArea textArea;
	private JLabel lblNewLabel;

	private String questionFilePath;
	private JTextArea textArea_1;

	/**
	 * Create the panel.
	 */
	public RoundAnswerPanel() {
		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(UIManager.getColor("Button.background"));
		panel.setForeground(Color.WHITE);
		add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		JButton btnNewButton = new JButton("Genera risposta");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				KitFrame frame = (KitFrame) SwingUtilities.getRoot(RoundAnswerPanel.this);

				String answer = RoundAnswerPanel.this.getAnswerTextArea()
						.getText();
				if (answer.trim().equals("")) {
					RoundAnswerPanel.this.showErrorDialog(
							"Inserisci una risposta", frame);
					return;
				}

				File firstHalfFile = RoundAnswerPanel.this.showSaveDialog(
						"Salva la prima metà",
						"Sono necessari entrambe i file", frame);
				if (firstHalfFile == null) {
					return;
				}

				File secondHalfFile = RoundAnswerPanel.this.showSaveDialog(
						"Salva la seconda metà",
						"Sono necessari entrambe i file", frame);
				if (secondHalfFile == null) {
					return;
				}

				try {
					File output = RoundAnswerPanel.this
							.tempFileBuilder(firstHalfFile.getName());
					output.createNewFile();

					Core core = Core.obtainSingleton();
					String hashedAnswer = core.buildAnswer(answer);
					core.encode(hashedAnswer, output);

					core.outputSplitter(firstHalfFile, secondHalfFile, output);
					output.delete();
					core.setCurrentAnswer(answer);
					frame.getGeneraRispostaMenuItem().setEnabled(false);
					frame.getMntmConcludiRound().setEnabled(true);
				} catch (IOException | InterruptedException | ShellException e1) {
					JOptionPane.showMessageDialog(frame, e1.getMessage(),
							"Errore", JOptionPane.ERROR_MESSAGE);
				}
			}

		});

		JButton btnCaricaDomanda = new JButton("Carica domanda");
		btnCaricaDomanda.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				KitFrame frame = (KitFrame) SwingUtilities.getRoot(RoundAnswerPanel.this);

				File firstHalfFile = RoundAnswerPanel.this.showLoadDialog(
						"Carica la prima metà",
						"Sono necessari entrambe i file", frame);
				if (firstHalfFile == null) {
					return;
				}

				File secondHalfFile = RoundAnswerPanel.this.showLoadDialog(
						"Carica la seconda metà",
						"Sono necessari entrambe i file", frame);
				if (secondHalfFile == null) {
					return;
				}

				try {
					String decodedQuestion = Core.obtainSingleton().decode(
							firstHalfFile.getAbsolutePath(),
							secondHalfFile.getAbsolutePath());
					RoundAnswerPanel.this.getPathLabel().setText(
							firstHalfFile.getName() + " - "
									+ secondHalfFile.getName());
					RoundAnswerPanel.this.getTDecodedQuestionTextArea()
							.setText(decodedQuestion);
				} catch (IOException | InterruptedException | ShellException e1) {
					RoundAnswerPanel.this.showErrorDialog(e1.getMessage(), frame);
				}

			}
		});
		panel.add(btnCaricaDomanda);
		panel.add(btnNewButton);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(UIManager.getColor("Button.background"));
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("max(120dlu;default):grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("max(64dlu;default):grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("max(65dlu;default):grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblDomanda_1 = new JLabel("Domanda:");
		panel_1.add(lblDomanda_1, "2, 2");

		lblNewLabel = new JLabel("Carica i file contententi la domanda.");
		lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
		panel_1.add(lblNewLabel, "4, 2");

		JLabel lblTestoInChiaro = new JLabel("Testo in chiaro:");
		panel_1.add(lblTestoInChiaro, "2, 4, default, top");

		textArea_1 = new JTextArea();
		textArea_1.setEditable(false);
		textArea_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.add(textArea_1, "4, 4, fill, fill");

		JLabel lblDomanda = new JLabel("Risposta:");
		panel_1.add(lblDomanda, "2, 6, default, top");

		textArea = new JTextArea();
		textArea.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.add(textArea, "4, 6, fill, fill");

	}

	protected JTextArea getAnswerTextArea() {
		return textArea;
	}

	public JLabel getPathLabel() {
		return lblNewLabel;
	}

	public JTextArea getTDecodedQuestionTextArea() {
		return textArea_1;
	}
}
